package rin.android.gamble;


import rin.android.database.ClassPlayer;
import rin.android.database.Database;
import rin.android.dialog.MessageDialog;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Register extends MyApp{
	
	EditText txtName;
	EditText txtMK;
	Context context;
	MessageDialog dialog;
	Database mData;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.register);
        mData = new Database(this);
        txtName = (EditText)findViewById(R.id.reg_fullname);
        txtMK = (EditText) findViewById(R.id.reg_password);
        TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
        Button register = (Button) findViewById(R.id.btnRegister);
        context = this;
        dialog = new MessageDialog();
        // Listening to Login Screen link
        loginScreen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				// Switching to Login Screen/closing register screen
				finish();
			}
		});
        register.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String userName = txtName.getText().toString().trim();
				String mk = txtMK.getText().toString().trim();
				mData.openDatabase();
				if(userName.equals("")){
					dialog.showNullDialog(context);
					return;
				}
				if(mk.equals("")){
					dialog.showNullDialog(context);
					return;
				}
				if(mData.isAvailable(userName)==1){
					ClassPlayer nPlayer = new ClassPlayer(userName, 10000, mk);
					mData.addPlayer(nPlayer);
					Intent i = new Intent(getApplicationContext(), MainActivity.class);
					i.putExtra("userName", userName);
					startActivity(i);
					finish();
				}
				else {
					dialog.showNotAvailableDialog(context);
				}
				mData.close();
			}
		});
    }
}
